﻿/***********************************************************************
 * <copyright file="SqlServerAccountDao.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   BangNC
 * Email:    BangNC@buca.vn
 * Website:
 * Create Date: Friday, March 14, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;


namespace DataObjects.ADO.SqlServer.Dictionary
{
    /// <summary>
    /// 
    /// </summary>
    public class SqlServerAccountDao : IAccountDao
    {
    

        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, Account> Make = reader =>
            new Account
            {
                AccountId = reader["AccountID"].AsInt(),               
                AccountCode = reader["AccountCode"].AsString(),
                AccountName = reader["AccountName"].AsString(),         
                ParentId = reader["ParentID"].AsInt(),//.AsIntForNull(),
                Grade = reader["Grade"].AsInt (),
                IsDetail = reader["IsDetail"].AsBool(),
                Description = reader["Description"].AsString(),             
                IsCustomer = reader["IsCustomer"].AsBool(),
                IsVendor = reader["IsVendor"].AsBool(),            
                IsInventoryItem = reader["IsInventoryItem"].AsBool(),
                IsFixedAsset = reader["IsFixedasset"].AsBool(),
                IsTax = reader["IsTax"].AsBool(),
                IsSystem = reader["IsSystem"].AsBool(),
                IsActive = reader["IsActive"].AsBool(),
                BalanceSide = reader["BalanceSide"].AsInt(),
                UnitId = reader["UnitID"].AsLong()
            };

        /// <summary>
        /// Takes the specified account.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <returns></returns>
        private object[] Take(Account account)
        {
            return new object[]  
            {
            "@AccountID", account.AccountId,      
            "@AccountCode" , account.AccountCode,
            "@AccountName" , account.AccountName,
            "@ParentID" , account.ParentId ,
            "@Grade" , account.Grade ,
            "@IsDetail" , account.IsDetail ,
            "@Description", account.Description ,
          
            "@IsCustomer" , account.IsCustomer ,
            "@IsVendor" , account.IsVendor ,
            "@IsTax" , account.IsTax ,
            "@IsInventoryItem" , account.IsInventoryItem ,
            "@IsFixedasset" , account.IsFixedAsset ,
            "@UnitID" , account.UnitId ,
            "@IsActive" , account.IsActive ,
            "@IsSystem" , account.IsSystem ,
             "@BalanceSide" , account.BalanceSide 
            };
        }

        

        public List<Account> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_Account";
            object[] parms = { "@WhereClause", null, "@OrderBy", "AccountCode", "@PageIndex", currentPage-1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_Account";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

        string IAccountDao.InsertAccount(Account Account)
        {
            const string sql = "uspInsert_Account";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(Account));
        }


        public string UpdateAccount(Account Account)
        {
            const string sql = "uspUpdate_Account";
            return Db.Update(sql, CommandType.StoredProcedure, Take(Account));
        }

        public string DeleteAccount(Account Account)
        {
            const string sql = @"uspDelete_Account";
            object[] parms = { "@AccountID", Account.AccountId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }


        public BusinessObjects.BussinessDictionary.Account GetAccount(int AccountId)
        {
            const string sql = @"uspGet_Account_ByID";
            object[] parms = { "@AccountID", AccountId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        public System.Collections.Generic.List<BusinessObjects.BussinessDictionary.Account> GetAccountsByParent(int parentId)
        {
            const string sql = @"uspGet_Account_ByCondition";
            object[] parms = { "@WhereClause", " WHERE ParentID = " + parentId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<Account> GetAccountsForComboTree(int accountId)
        {
            //const string sql = @"uspGet_Account_ForComboTreee";
            //object[] parms = { "@AccountID", accountId };
            //return Db.ReadList(sql, true, Make, parms);

            const string sql = @"uspGet_Account_ByUnit";
            object[] parms = { "@UnitID", 1 };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }


        public List<Account> GetAccountsForIsInventoryItem(int unitId)
        {
            const string sql = @"uspGet_Account_ByIsInventoryItem";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }


        public List<Account> GetAccountsForIsTax(int unitId)
        {
            const string sql = @"uspGet_Account_ByIsTax";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<Account> GetAccountsForIsAccountingObject(int unitId)
        {
            const string sql = @"uspGet_Account_ByIsAccountingObject";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }


        public List<Account> GetAccountsForIsFixedasset(int unitId)
        {
            const string sql = @"uspGet_Account_ByIsFixedasset";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }


        public List<Account> GetAccountsByUnit(int unitId)
        {
            const string sql = @"uspGet_Account_ByUnit";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }


        public string InsertAccountByUnit(int unitId)
        {
            const string sql = @"uspInsert_Account_ByUnit";
            object[] parms = { "@UnitID", unitId };
            return Db.Insert(sql, CommandType.StoredProcedure, parms);
        }
    }
}
